using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Threading;
namespace MicroMsg.UI.UserContrl
{
	public class GifControl : Control
	{
		public const string TAG = "GifControl";
		public const string ImagePart = "Image";
		private DispatcherTimer _animationTimer;
		private Image _image;
		private int _animationFrameIndex;
		private List<GifFrame> _frames = new List<GifFrame>();
		private bool isGif;
		public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(string), typeof(GifControl), null);
		public static readonly DependencyProperty CoverProperty = DependencyProperty.Register("Cover", typeof(string), typeof(GifControl), null);
		public static readonly DependencyProperty StretchProperty = DependencyProperty.Register("Stretch", typeof(Stretch), typeof(GifControl), new PropertyMetadata(0));
		private bool _contentLoaded;
		public event EventHandler OnStartPlay;
		public event EventHandler OnStopPlay;
		public string Source
		{
			get
			{
				return (string)base.GetValue(GifControl.SourceProperty);
			}
			set
			{
				string text = base.GetValue(GifControl.SourceProperty) as string;
				if (text == value)
				{
					return;
				}
				base.SetValue(GifControl.SourceProperty, value);
				this.OnSourceChanged();
			}
		}
		public string Cover
		{
			get
			{
				return (string)base.GetValue(GifControl.CoverProperty);
			}
			set
			{
				base.SetValue(GifControl.CoverProperty, value);
				this.OnCoverChanged();
			}
		}
		public Stretch Stretch
		{
			get
			{
				return (Stretch)base.GetValue(GifControl.StretchProperty);
			}
			set
			{
				base.SetValue(GifControl.StretchProperty, value);
			}
		}
		public GifControl()
		{
			this.InitializeComponent();
			this._animationTimer = new DispatcherTimer();
			this._animationTimer.add_Tick(new EventHandler(this.timer_Tick));
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this._image = (base.GetTemplateChild("Image") as Image);
			this.OnCoverChanged();
			this.OnSourceChanged();
		}
		protected virtual void OnSourceChanged()
		{
			if (this._image != null && !string.IsNullOrEmpty(this.Source))
			{
				if (string.IsNullOrEmpty(this.Cover))
				{
					this.LoadImage(this.Source);
					return;
				}
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.LoadImage(this.Source);
				});
			}
		}
		protected virtual void OnCoverChanged()
		{
			if (this._image != null && !string.IsNullOrEmpty(this.Cover))
			{
				this.LoadImage(this.Cover);
			}
		}
		public bool IsGif()
		{
			return this.isGif;
		}
		private void LoadImage(string imageSource)
		{
			if (!string.IsNullOrEmpty(imageSource))
			{
				if (imageSource.StartsWith("/Resource"))
				{
					imageSource = imageSource.Substring(1);
				}
				Stream stream;
				if (imageSource.StartsWith("Resource"))
				{
					Uri uri = new Uri(imageSource, 2);
					StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
					if (resourceStream == null)
					{
						return;
					}
					stream = resourceStream.get_Stream();
				}
				else
				{
					byte[] array = StorageIO.readFromFile(imageSource);
					stream = new MemoryStream(array);
				}
				this.isGif = GifControl.isGifPic(stream);
				if (this.isGif)
				{
					this.Stop();
					List<GifFrame> list = new List<GifFrame>();
					GifDecoder gifDecoder = new GifDecoder(stream);
					gifDecoder.decode(list);
					if (list.get_Count() > 0)
					{
						this._frames.Clear();
						this._frames.AddRange(list);
					}
					this.AnimateImage();
					this.Start();
					return;
				}
				if (this._image != null)
				{
					BitmapImage bitmapImage = new BitmapImage();
					bitmapImage.SetSource(stream);
					this._image.set_Source(bitmapImage);
				}
			}
		}
		private static bool isGifPic(Stream imgStream)
		{
			if (imgStream == null)
			{
				return false;
			}
			imgStream.set_Position(0L);
			byte[] array = new byte[100];
			imgStream.Read(array, 0, 100);
			ImageFromat imageFormat = ImageUtil.getImageFormat(array);
			imgStream.set_Position(0L);
			return imageFormat == ImageFromat.const_4;
		}
		private void timer_Tick(object sender, EventArgs e)
		{
			this.AnimateImage();
		}
		private void AnimateImage()
		{
			if (this._animationFrameIndex < this._frames.get_Count())
			{
				GifFrame gifFrame = this._frames.get_Item(this._animationFrameIndex);
				if (gifFrame.bitmap != null)
				{
					if (this._image != null)
					{
						this._image.set_Source(gifFrame.bitmap);
					}
					this._animationTimer.set_Interval(new TimeSpan(0, 0, 0, 0, gifFrame.duration));
					this._animationFrameIndex++;
					if (this._animationFrameIndex >= this._frames.get_Count())
					{
						this._animationFrameIndex = 0;
					}
				}
			}
		}
		public void Start()
		{
			if (string.IsNullOrEmpty(this.Source))
			{
				return;
			}
			this._animationTimer.Start();
			EventHandler onStartPlay = this.OnStartPlay;
			if (onStartPlay != null)
			{
				onStartPlay.Invoke(this, null);
			}
		}
		public void Stop()
		{
			try
			{
				this._animationFrameIndex = 0;
				this._animationTimer.Stop();
				EventHandler onStopPlay = this.OnStopPlay;
				if (onStopPlay != null)
				{
					this.OnStopPlay.Invoke(this, null);
				}
			}
			catch (Exception)
			{
				Log.smethod_1("GifControl", "GIf Stop Err");
			}
		}
		public void Release()
		{
			if (this._frames != null)
			{
				this._frames.Clear();
			}
			this.isGif = false;
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Util/Gif/GifControl.xaml", 2));
		}
	}
}
